import { Project } from '@entity/project';
import { FriendInfo, User } from '@entity/user';
import { SystemMsg } from '@entity/system_msg';
import { Attention } from '@entity/chat';
import user_axios from '@axios/user';
import { getMe, getProjectMap, getUserMap, getPrivateMsgMap, storageMe, getSystemMsgMap, getFriendMap, storageUser } from './utils/functions';

export default {
    webName: () => (): string => 'Olah!!!',

    project         : (state: any) => (id: number)      : Project                   => getProjectMap(state).get(id),
    projectMap      : (state: any) => ()                : Map<number, Project>      => getProjectMap(state),
    projectList     : (state: any) => ()                : Array<Project> 	        => getProjectMap(state).valueArray(),

    attention       : (state: any) => (id: number)      : Attention                 => getPrivateMsgMap(state).get(id),
    attentionMap    : (state: any) => ()                : Map<number, Attention>    => getPrivateMsgMap(state),
    attentionList   : (state: any) => ()                : Array<Attention> 	        => getPrivateMsgMap(state).valueArray(),

    systemMsg       : (state: any) => (id: number)      : SystemMsg 		        => getSystemMsgMap(state).get(id),
    systemMsgMap    : (state: any) => ()                : Map<number, SystemMsg>    => getSystemMsgMap(state),
    systemMsgList   : (state: any) => ()                : Array<SystemMsg> 		    => getSystemMsgMap(state).valueArray(),

    friend          : (state: any) => (id: number)      : FriendInfo 		        => getFriendMap(state).get(id),
    friendMap       : (state: any) => ()                : Map<number, FriendInfo>   => getFriendMap(state),
    friendList      : (state: any) => ()                : Array<FriendInfo> 		=> getFriendMap(state).valueArray(),

    user            : (state: any) => (id: number)      : User                      => {
        let result: User = getUserMap(state).get(id);
        String.prototype.debounce(() => {
            user_axios.base.queryById(id).then((res: any) => {
                result = res.user;
                storageUser(state, result);
            });
        }, 500);
        return result;
    },
    userMap         : (state: any) => ()                : Map<number, User> 		=> getUserMap(state),
    userList        : (state: any) => ()                : Array<User> 		        => getUserMap(state).valueArray(),

    /**
     * 获取我自己的信息, deep 表示是否深拷贝
     */
    me: (state: any) => (deep: boolean = false): User => {
        let me: User = getMe(state);
        if (me == undefined) {
            return undefined;
        }
        return deep ? String.prototype.deepCopy(me) : me;
    },
    /**
     * 更新我的个人信息, 并返回深拷贝之后的对象
     */
    updateMe: (state: any) => (me: User): User => {
        storageMe(state, me);
        return String.prototype.deepCopy(me);
    }
}